package hedron.dialog;

import hedron.Application;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;

public abstract class AbstractDialog extends Dialog {

	String title = "";
	String message = "";

	Label titleLabel;
	int messageLevel;
	Label messageImage;
	Label messageLabel;
	Image titleAreaImage;

	protected AbstractDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite grid = new Composite(parent, 0);
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 1;
			layout.marginLeft = 0;
			layout.marginRight = 0;
			layout.marginTop = 0;
			layout.marginBottom = 0;
			layout.marginWidth = 0;
			layout.marginHeight = 0;
			grid.setLayout(layout);
		}
		{
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			gd.horizontalIndent = 0;
			gd.verticalIndent = 0;
			grid.setLayoutData(gd);
		}

		createHeader(grid);
		{
			Composite content = new Composite(grid, 0);
			{
				GridLayout layout = new GridLayout();
				layout.numColumns = 1;
				layout.marginLeft = 0;
				layout.marginRight = 0;
				layout.marginTop = 0;
				layout.marginBottom = 0;
				layout.marginWidth = 0;
				layout.marginHeight = 0;
				content.setLayout(layout);
			}
			{
				GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
				gd.horizontalIndent = 0;
				gd.verticalIndent = 0;
				content.setLayoutData(gd);
			}
			createContent(content);
		}

		{
			Label label = new Label(grid, SWT.SEPARATOR | SWT.HORIZONTAL);
			label.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false));
		}
		createFooter(grid);
		return grid;
	}

	abstract void createContent(Composite parent);

	abstract void createFooter(Composite parent);

	// We allow the user to resize this dialog
	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	public int open() {
		if (getContents() == null) {
			create();
		}
		getContents().pack();
		Application.placeDialogInCenter(getParentShell(), getShell());
		return super.open();
	}

	void createHeader(Composite parent) {
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
		Composite grid = new Composite(parent, 0);
		{
			GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
			gd.horizontalIndent = 0;
			gd.verticalIndent = 0;
			grid.setLayoutData(gd);
		}
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			layout.marginLeft = 0;
			layout.marginRight = 0;
			layout.marginTop = 0;
			layout.marginBottom = 0;
			layout.marginWidth = 0;
			layout.marginHeight = 0;
			grid.setLayout(layout);
		}
		grid.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		{
			Composite left = new Composite(grid, 0);
			left.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
			{
				GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
				gd.horizontalAlignment = SWT.FILL;
				left.setLayoutData(gd);
			}
			{
				GridLayout layout = new GridLayout();
				layout.numColumns = 2;
				layout.marginLeft = 0;
				layout.marginRight = 0;
				layout.marginTop = 0;
				layout.marginBottom = 0;
				layout.marginWidth = 0;
				layout.marginHeight = 0;
				left.setLayout(layout);
			}
			{
				Label label = new Label(left, 0);

				Font font = label.getFont();
				FontData[] fontData = font.getFontData();
				fontData[0].setStyle(SWT.BOLD);
				label.setFont(new Font(parent.getDisplay(), fontData[0]));
				GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
				gd.horizontalIndent = 6;
				gd.verticalIndent = 10;
				gd.horizontalSpan = 2;
				label.setLayoutData(gd);
				label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
				titleLabel = label;
				titleLabel.setText(title);
			}
			{
				Label label = new Label(left, 0);
				GridData gd = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
				gd.horizontalIndent = 6;
				gd.verticalIndent = 5;
				gd.heightHint = 25;
				gd.widthHint = 16;
				label.setLayoutData(gd);
				label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
				messageImage = label;
			}
			{
				Label label = new Label(left, 0);
				GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
				gd.verticalIndent = 10;
				gd.heightHint = 25;
				label.setLayoutData(gd);
				label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
				messageLabel = label;
				messageLabel.setText(message);
			}
		}
		{
			Image image = JFaceResources.getImage(TitleAreaDialog.DLG_IMG_TITLE_BANNER);
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
			gd.horizontalIndent = 0;
			gd.verticalIndent = 0;
			label.setLayoutData(gd);
			label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
			label.setImage(image);
		}
		toolkit.dispose();
	}

	public void setTitle(String newTitle) {
		String title = newTitle;
		if (title == null) title = "";
		titleLabel.setText(title);
	}

	public void setMessage(String newMessage, int imageLevel) {
		if (newMessage == null) {
			newMessage = "";
		}
		message = newMessage;
		messageLabel.setText(message);

		Image newImage;
		if (imageLevel == 1) {
			newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_WARNING);
		} else if (imageLevel == 2) {
			newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_ERROR);
		} else {
			newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_INFO);
		}
		messageImage.setImage(newImage);
	}
}
